package ua.edu.test.tab2mycards;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import ua.edu.test.MainActivity;
import ua.edu.test.R;
import ua.edu.test.db.DBWork;

/**
 * Created by Max on 15.07.2014.
 */
public class AddCard extends android.support.v4.app.Fragment {

	public static final String LOG_TAG = AddCard.class.getName();

	EditText editTxtLastFourDigCard;
	EditText editTxtUserCardName;
	Spinner spinnerChooseBank;

    Integer[] banks_icons = {R.drawable.privat, R.drawable.unicredit, R.drawable.exim, R.drawable.aval, R.drawable.kredo, R.drawable.marfin,
            R.drawable.pravex, R.drawable.profin, R.drawable.pumb, R.drawable.ukrgas, R.drawable.ukrin, R.drawable.ukrsib};

	@Override
	public View onCreateView(LayoutInflater inflater,
							 ViewGroup container,
							 Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_tab2_mycard_add_card, container, false);


		TextView txtCardNumber = (TextView)view.findViewById(R.id.txtLastFourDigCard);
		txtCardNumber.setText("Введите последние 4 цифры номера Вашей карты");

		TextView txtChooseBank = (TextView)view.findViewById(R.id.txtChooseBank);
		txtChooseBank.setText("Выберите банк");

		TextView txtCardName = (TextView)view.findViewById(R.id.txtUserCardName);
		txtCardName.setText("Введите название карты");



		editTxtLastFourDigCard = (EditText)view.findViewById(R.id.editTxtLastFourDigCard);

		editTxtUserCardName = (EditText)view.findViewById(R.id.editTxtUserCardName);

		spinnerChooseBank = (Spinner)view.findViewById(R.id.spinnerChooseBank);

		Button btnAddCard = (Button)view.findViewById(R.id.btnAddCard);
		btnAddCard.setText("Добавить");
		btnAddCard.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				DBWork.addCard(editTxtLastFourDigCard.getText().toString(), editTxtUserCardName.getText().toString(), spinnerChooseBank.getSelectedItem().toString());
				MainActivity mainActivity = (MainActivity) getActivity();
				mainActivity.showTab1Action();
			}
		});

        SpinnerAdapter adapter = new SpinnerAdapter(getActivity(),
                R.layout.spinner_row, getResources().getStringArray(R.array.banks));

        // Вызываем адапетр
        spinnerChooseBank.setAdapter(adapter);
        spinnerChooseBank.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

		return view;
	}

    public class SpinnerAdapter extends ArrayAdapter<String> {

        public SpinnerAdapter(Context context, int textViewResourceId,
                              String[] objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                                  ViewGroup parent) {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View row = inflater.inflate(R.layout.spinner_row, parent, false);
            TextView label = (TextView) row.findViewById(R.id.bank);
            label.setText(getResources().getStringArray(R.array.banks)[position]);

            ImageView icon = (ImageView) row.findViewById(R.id.icon);
            icon.setImageResource(banks_icons[position]);

            return row;
        }


}
}
